package paas.storage.serviceTest;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mortbay.util.ajax.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import paas.storage.HbaseApiApplication;
import paas.storage.distributedColumnDatabase.IConnection;
import paas.storage.distributedColumnDatabase.IData;
import paas.storage.distributedColumnDatabase.IManagement;
import paas.storage.distributedColumnDatabase.ITable;
import paas.storage.dto.DataQueryVo;
import paas.storage.result.*;
import paas.storage.util.JSONHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static paas.storage.distributedColumnDatabase.impl.IDataImpl.showResult;

/**
 * 备注
 *
 * @author xufeng
 * @email 525207937@qq.com
 * @date 2021/1/26 14:38
 */
@Service
public class IDataTest {

    private static Logger log = LoggerFactory.getLogger(IDataTest.class);

    @Autowired
    private IData iData;
    @Autowired
    private ITable iTable;
    @Autowired
    private IConnection iConnection;
    @Autowired
    private IManagement iManagement;

    /**
     * 测试数据操作汇总
     * 1.插入数据
     * 2.删除数据
     * 3.修改列簇信息
     * 4.查询数据接口 只查一个用户列
     * 5.根据键值查询 queryByKey
     * 5.根据范围查询多过滤器
     */
    public void all() throws InterruptedException {
        Map map = (Map) JSONHelper.ResolveJsonFileToObject("test.json");

        //创建连接
        CreateResponse createResponse = iConnection.create("", map.get("token").toString(), map.get("config").toString());
        //618532e7-e1f3-4ffd-83d0-3a23c56318bb
        String connectionId = createResponse.getConnectionId();

        Map<String, Object> insertData = (Map<String, Object>) map.get("insertData");
        //插入数据
        String database = insertData.get("database").toString();
        String table = insertData.get("table").toString();
        String columnFamilyList = insertData.get("columnFamilyList").toString();
        //1.插入数据
        log.info(" 5.2.3.1数据插入参数,{}，{}，{}，{}，{}，", connectionId, database, table, columnFamilyList, "");
        DataInsertResponse dataInsertResponse = iData.insert(connectionId, database, table, columnFamilyList, "");
        log.info("5.2.3.1插入结果,{}", JSON.toString(dataInsertResponse));

        String conditions = "{}";
        String startKey = "";
        String endKey = "";
        columnFamilyList = "";
        DataQueryResponse DataQueryResponse = iData.query(connectionId, database, table, conditions, startKey, endKey, columnFamilyList, "");
        log.info("5.2.3.1插入查询结果{},", JSON.toString(DataQueryResponse.getResult()));
        Map<String, Object> delData = (Map<String, Object>) map.get("delData");

        String rowKey = delData.get("rowKey").toString();
        columnFamilyList = delData.get("columnFamilyList").toString();
        log.info("5.2.3.2删除数据参数{}，{}，{}，{}，{}，{},", connectionId, database, table, rowKey, columnFamilyList, "");
        Thread.sleep(500);
        Response response = iData.delete(connectionId, database, table, rowKey, columnFamilyList, "");
        log.info("5.2.3.2删除结果{},", response.toString());
        //2.删除数据
        DataQueryResponse DataQueryResponse1 = iData.query(connectionId, database, table, conditions, startKey, endKey, columnFamilyList, "");
        log.info("5.2.3.2删除查询结果{},", JSON.toString(DataQueryResponse1.toString()));
        Map<String, Object> updateData = (Map<String, Object>) map.get("updateData");
        //修改列簇信息
        columnFamilyList = updateData.get("columnFamilyList").toString();
        log.info("5.2.3.3修改数据参数{}，{}，{}，{}，{}，{},", connectionId, database, table, rowKey, columnFamilyList, "");
        //3.修改列簇信息
        Response response1 = iData.update(connectionId, database, table, columnFamilyList, "");
        log.info("5.2.3.3修改结果{},", response1.toString());
        //立刻查询
        Map<String, Object> selectData = (Map<String, Object>) map.get("selectData");

        columnFamilyList = selectData.get("columnFamilyList").toString();
        conditions = "{}";
        DataQueryResponse DataQueryResponse2 = iData.query(connectionId, database, table, conditions, startKey, endKey, columnFamilyList, "");
        log.info("5.2.3.3查询结果{},", JSON.toString(DataQueryResponse2.getResult()));

        Map<String, Object> selectDataByConditions = (Map<String, Object>) map.get("selectDataByConditions");

        //4.查询数据接口 只查一个用户列
        startKey = selectDataByConditions.get("startKey").toString();
        endKey = selectDataByConditions.get("endKey").toString();
        columnFamilyList = selectDataByConditions.get("columnFamilyList").toString();
        //查询user age小于50的 并且列簇为user下的所有列
        //new CompareFilter.CompareOp.LESS_OR_EQUAL
        conditions = selectDataByConditions.get("conditions").toString();;

        log.info("数据查询参数{}，{}，{}，{}，{}，,{},{},{},", connectionId, database, table, conditions, startKey,endKey,columnFamilyList, "");
        DataQueryResponse response2 = iData.query(connectionId, database, table, conditions, startKey, endKey, columnFamilyList, "");
        log.info("查询结果{},", JSON.toString(response2.getResult()));

        Map<String, Object> queryByKey = (Map<String, Object>) map.get("queryByKey");

        //根据rowkey查询
        String rowkey = queryByKey.get("rowkey").toString();
        //5.根据键值查询 queryByKey
        columnFamilyList = queryByKey.get("columnFamilyList").toString();
        log.info("根据rowkey查询数据参数{}，{}，{}，{}，{}，,{}", connectionId, database, table, rowkey, columnFamilyList, "");
        DataqueryByKeyResponse dataqueryByKeyResponse = iData.queryByKey(connectionId, database, table, rowkey, columnFamilyList, "");
        // 拿到Result对象
        List<DataQueryVo> dataQueryVo1 = showResult(dataqueryByKeyResponse.getIteration());
        log.info("根据rowkey查询数据{}",JSON.toString(dataQueryVo1));

        Map<String, Object> queryByVersion = (Map<String, Object>) map.get("queryByVersion");

        //先查下一下所有数据
        startKey = queryByVersion.get("startKey").toString();
        endKey = queryByVersion.get("endKey").toString();
        columnFamilyList = queryByVersion.get("columnFamilyList").toString();
        //查询多版本数据
        conditions = queryByVersion.get("conditions").toString();
        log.info("根据rowkey查询数据参数{}，{}，{}，{}，{}，,{},{},{}", connectionId, database, table, conditions, startKey, endKey,columnFamilyList, "");
        DataQueryResponse response3 = iData.query(connectionId, database, table, conditions, startKey, endKey, columnFamilyList, "");
        log.info("查询结果{},", JSON.toString(response3.getResult()));

        Map<String, Object> queryByFilter = (Map<String, Object>) map.get("queryByFilter");

        //根据范围查询 查询数据大于等于30的   列簇=user 列=age的
        //TimeRange=范围时间段,注意后面是当前时间戳 或者更后面的时间才能查询出数据,注意组合效果都要满足才行",
        //
        String filter = queryByFilter.get("filter").toString();
        //5.根据范围查询多过滤器
        log.info("根据范围查询多过滤器参数{}，{}，{}，{}，{}", connectionId, database, table, filter, "");
        DataQueryByRangeResponse dataQueryByRangeResponse = iData.queryByRange(connectionId, database, table, filter, "");
        // 进行迭代，拿到Result对象
        List<DataQueryVo> dataQueryVos = new ArrayList<>();
        while (dataQueryByRangeResponse.iteration.hasNext()) {
            Result result = dataQueryByRangeResponse.iteration.next();
            List<DataQueryVo> dataQueryVo = showResult(result);
            dataQueryVos.addAll(dataQueryVo);
        }
        log.info("5.2.3.6返回迭代器对象,{}", JSON.toString(dataQueryByRangeResponse));
        log.info("5.2.3.6查询结果{},", dataQueryVos.toArray());

    }
}
